package com.xjrsoft.module.visual.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.module.base.dto.GetPageListDto;
import com.xjrsoft.module.base.entity.XjrBaseRole;
import com.xjrsoft.module.base.service.IXjrBaseRoleService;
import com.xjrsoft.module.visual.dto.VisualReleaseRelationDto;
import com.xjrsoft.module.visual.entity.XjrVisualReleaseRelation;
import com.xjrsoft.module.visual.service.IXjrVisualReleaseRelationService;
import com.xjrsoft.module.visual.vo.VisualReleaseHomeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * BI发布权限表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-03-15
 */
@RestController
@AllArgsConstructor
@RequestMapping("/visual-relation")
@Api(value = "/visual-relation", tags = "BI看板发布权限模块")
public class XjrVisualReleaseRelationController {

    private final IXjrVisualReleaseRelationService releaseRelationService;

    private final IXjrBaseRoleService roleService;

    @GetMapping("/{objectId}/obj/{category}")
    @ApiOperation("查看用户/角色已授权的BI看板")
    public Response<List<VisualReleaseHomeVo>> getVisualOfRelationObject(@PathVariable String objectId, @PathVariable Integer category) {
        return Response.ok(releaseRelationService.getVisualOfRelationObject(objectId, category));
    }

    @GetMapping("/current-visual")
    @ApiOperation("查看当前用户已授权的BI看板")
    public Response<PageOutput<VisualReleaseHomeVo>> getVisualOfRelationObject(GetPageListDto dto) {
        String userId = SecureUtil.getUserId();
        List<XjrBaseRole> roleList = roleService.getRolesByUserId(userId);
        List<String> roleIds = null;
        if (CollectionUtil.isNotEmpty(roleList)) {
            roleIds = roleList.stream().map(role -> role.getRoleId()).collect(Collectors.toList());
        }
        roleIds.add("defaultCurrent");
        return Response.ok(releaseRelationService.getRelationVisualOfUser(userId, roleIds, dto));
    }

    @PostMapping
    @ApiOperation("添加用户/角色BI看板的授权")
    public Response addVisualReleaseRelation(@RequestBody VisualReleaseRelationDto dto) {
        int count = releaseRelationService.count(Wrappers.<XjrVisualReleaseRelation>query().lambda()
                .eq(XjrVisualReleaseRelation::getVisualReleaseId, dto.getVisualReleaseId())
                .eq(XjrVisualReleaseRelation::getObjectId, dto.getObjectId()).eq(XjrVisualReleaseRelation::getCategory, dto.getCategory()));
        if (count > 0) {
            return Response.status(false);
        }
        XjrVisualReleaseRelation visualReleaseRelation = BeanUtil.copy(dto, XjrVisualReleaseRelation.class);
        return Response.status(releaseRelationService.save(visualReleaseRelation));
    }

    @DeleteMapping
    @ApiOperation("删除用户/角色BI看板的授权")
    public Response deleteVisualReleaseRelation(@RequestBody VisualReleaseRelationDto dto) {
        Wrapper<XjrVisualReleaseRelation> wrapper = Wrappers.<XjrVisualReleaseRelation>query().lambda()
                .eq(XjrVisualReleaseRelation::getVisualReleaseId, dto.getVisualReleaseId())
                .eq(XjrVisualReleaseRelation::getObjectId, dto.getObjectId())
                .eq(XjrVisualReleaseRelation::getCategory, dto.getCategory());
        return Response.status(releaseRelationService.remove(wrapper));
    }
}
